package com.hanserwei.community.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hanserwei.community.entity.common.ApiResponse;
import com.hanserwei.community.entity.po.Owner;
import com.hanserwei.community.entity.request.OwnerQueryRequest;
import com.hanserwei.community.entity.vo.OwnerVO;
import com.hanserwei.community.enums.ResponseCodeEnum;
import com.hanserwei.community.service.OwnerService;
import com.hanserwei.community.uitils.ThrowUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/owner")
@Tag(name = "OwnerController")
@RequiredArgsConstructor
public class OwnerController {

    private final OwnerService ownerService;

    @Operation(summary = "添加业主信息", description = "新增业主并绑定到指定房屋，房屋会自动关联所属楼栋")
    @PostMapping("/add")
    public ApiResponse<Boolean> saveOwner(@RequestBody Owner owner){
        ThrowUtils.throwIf(owner == null, ResponseCodeEnum.INVALID_PARAMETER,"参数错误！");
        boolean result = ownerService.saveOwner(owner);
        return ApiResponse.success("添加成功！",result);
    }

    @Operation(summary = "删除业主信息", description = "根据业主ID删除，删除后房屋可重新分配")
    @DeleteMapping("/delete")
    public ApiResponse<Boolean> deleteOwner(@Parameter(description = "要删除的业主ID") @RequestParam("id") Integer id){
        ThrowUtils.throwIf(id == null || id <= 0, ResponseCodeEnum.INVALID_PARAMETER, "业主ID参数错误！");
        boolean result = ownerService.removeById(id);
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "删除失败！");
        return ApiResponse.success("删除成功！",true);
    }

    @Operation(summary = "更新业主信息", description = "更新业主基本信息及绑定房屋")
    @PostMapping("/update/{id}")
    public ApiResponse<Boolean> updateOwner(@RequestBody Owner owner, @PathVariable Integer id){
        ThrowUtils.throwIf(owner == null, ResponseCodeEnum.INVALID_PARAMETER,"参数错误！");
        owner.setId(id);
        boolean result = ownerService.updateOwner(owner);
        return ApiResponse.success("更新成功！",result);
    }

    @Operation(summary = "查询业主详情", description = "根据业主ID查询详情，包含关联的房屋与楼栋信息")
    @GetMapping("/{id}")
    public ApiResponse<OwnerVO> queryOwner(@Parameter(description = "要查询的业主ID") @PathVariable("id") Integer id){
        OwnerVO ownerVO = ownerService.getOwnerVOById(id);
        return ApiResponse.success("查询成功！",ownerVO);
    }

    @Operation(summary = "分页查询业主信息", description = "支持按姓名、电话、房屋以及楼栋等条件组合查询")
    @PostMapping("/search")
    public ApiResponse<Page<OwnerVO>> listOwner(@RequestBody OwnerQueryRequest request){
        ThrowUtils.throwIf(request == null, ResponseCodeEnum.INVALID_PARAMETER,"参数错误！");
        Page<OwnerVO> result = ownerService.listByPage(request);
        return ApiResponse.success("查询成功！",result);
    }

    @Operation(summary = "批量删除业主", description = "根据业主ID列表批量删除，删除后房屋可重新分配")
    @DeleteMapping("/deleteBatch")
    public ApiResponse<Boolean> deleteBatch(@Parameter(description = "批量删除业主信息") @RequestBody Integer[] ids){
        ThrowUtils.throwIf(ids == null || ids.length == 0, ResponseCodeEnum.INVALID_PARAMETER, "参数错误！");
        boolean result = ownerService.removeBatchByIds(List.of(ids));
        ThrowUtils.throwIf(!result, ResponseCodeEnum.SYSTEM_ERROR, "删除失败！");
        return ApiResponse.success("删除成功！", true);
    }
}
